<?php
/*
Plugin ID: cn.usephp.filemgr
Plugin Name: 文件管理器
Plugin URI: http://www.usephp.cn/plugins/filemgr.html
Description: 提供基于ftp,ssh2协议的文件管理功能，包括上传，删除，压缩，解压等。同时可以通过此插件对系统自身进行升级。<br/>注:安装启用此插件可能会给系统带来一定的风险。
Author: Leo Ning
Version: 1.0
Author URI: http://www.usephp.cn/
Option: filemgr
*/
bind ( 'get_desktop_applications', '__the_filemgr_app', 2 );
function __the_filemgr_app($apps) {
	$apps [990] = array ('id' => "filemgr", 'url' => ADMIN_URL . 'files/', 'cls' => 'icon_files', 'name' => '文件管理', 'tip' => '文件管理器', 'icon' => ADMIN_URL . 'files/images/files.png' );
	return $apps;
}
bind ( 'desktop_styles', '__the_filemgr_icon' );
function __the_filemgr_icon($icons) {
	$icons .= ".icon_files { background: url(files/images/files2.png) no-repeat 0 50%; }\n";
	return $icons;
}
//添加配置选项
bind ( 'get_core_option_group', 'get_core_option_group_for_filesystem' );
function get_core_option_group_for_filesystem($groups) {
	$groups ['filemgr'] = '<i class="icon-briefcase"></i>文件系统';
	return $groups;
}
//读取缓存配置 
bind ( 'get_option_filemgr', 'get_option_filemgr_option' );
function get_option_filemgr_option($options) {
	if (file_exists ( APPDATA_PATH . 'filemgr.ini' )) {		
		$opts = parse_ini_file ( APPDATA_PATH . 'filemgr.ini' );
		if ($opts) {
			$options = array_merge ( $options, $opts );
		}
	}
	return $options;
}
//保存配置
bind ( 'on_save_option_filemgr', 'on_save_option_filemgr' );
function on_save_option_filemgr($options) {
	$opts = array ('[filemgr]' );
	$filemgrs = $options ['filemgr'];
	foreach ( $filemgrs as $key => $val ) {
		$opts [] = "\t{$key} = $val";
	}
	$rst = @file_put_contents ( APPDATA_PATH . 'filemgr.ini', implode ( "\n", $opts ) );
	if ($rst) {
		return true;
	} else {
		return '无法保存缓存配置文件.';
	}
}
//配置模板
bind ( 'get_filemgr_option_tpl', 'get_filemgr_option_tpl', 1, 2 );
function get_filemgr_option_tpl($tpl, $options) {
	global $__global;
	$__global ['ftp_enabled'] = extension_loaded ( 'ftp' );
	$__global ['ssh2_enabled'] = extension_loaded ( 'ssh2' );
	
	return 'core/views/files/fileoption.tpl';
}
bind ( 'get_filesystem', 'create_filemgr_filesystem' );
function create_filemgr_filesystem($fs) {
	if (is_null ( $fs ) && file_exists ( APPDATA_PATH . 'filemgr.ini' )) {
		$opts = parse_ini_file ( APPDATA_PATH . 'filemgr.ini' );
		if ($opts) {
			$fsClz = 'FileSystem' . $opts ['type'];
			//此处需要使用session中使用输入的用户名和密码
			$opts ['port'] = intval ( $opts ['port'] );
			$opts ['username'] = sess_get ( 'fs_username', '' );
			$opts ['password'] = sess_get ( 'fs_password', '' );
			if ($opts ['username'] && $opts ['password'] && class_exists ( $fsClz )) {
				$fs = new $fsClz ( $opts );
				if (! $fs->connect ()) {
					return false;
				}
			}			
		}else{
			return false;
		}
	}
	return $fs;
}
bind('desktop_foot','filesmgr_desktop_foot');
function filesmgr_desktop_foot($fragment){
	echo '<script type="text/javascript" src="',ADMIN_URL,'files/transit.js"></script>';	
	$fragment .= 'dddd';
	return $fragment;
}
